﻿#include "ReadMEMSDataCodec_Server.h"

#include "Mapper_MEMSData.h"
#include "TcpCodec.h"

#include <qdebug.h>

ReadMEMSDataCodec_Server::ReadMEMSDataCodec_Server(QObject *parent) : QObject(parent)
{
  decode_fun[ReadAll]=&ReadMEMSDataCodec_Server::decode_readAll;
  decode_fun[ReadByDateTime]=&ReadMEMSDataCodec_Server::decode_ReadByDateTime;
}

ReadMEMSDataCodec_Server::~ReadMEMSDataCodec_Server()
{

}

QByteArray ReadMEMSDataCodec_Server::code(uchar readType,QByteArray data)
{
  data.prepend(readType);
  return data;
}

QByteArray ReadMEMSDataCodec_Server::decode(QByteArray data)
{
  unsigned char readType=data[0];
  if(ReadType_Size<=readType)
    {
      return QByteArray();
    }
  else
    {
      return (this->*(decode_fun[readType]))(data.mid(1));
    }
}

QByteArray ReadMEMSDataCodec_Server::decode_readAll(QByteArray data)
{
  QVector<MEMSData> list=mapper_MEMSData->getAllMEMSData();
  QByteArray result;
  for(MEMSData mEMSData:list)
    {
      result.append(mEMSData.toByteArray());
    }
  return result.prepend(ReadAll);
}

QByteArray ReadMEMSDataCodec_Server::decode_ReadByDateTime(QByteArray data)
{
  //  QVector<MEMSData> list=
  return QByteArray();
}

Mapper_MEMSData* ReadMEMSDataCodec_Server::getMapper_MEMSData()
{
  return mapper_MEMSData;
}

void ReadMEMSDataCodec_Server::setMapper_MEMSData(Mapper_MEMSData* mapper_MEMSData)
{

  this->mapper_MEMSData=mapper_MEMSData;
  printInjection(this,mapper_MEMSData);
}

TcpCodec* ReadMEMSDataCodec_Server::getTcpCodec()
{
  return tcpCodec;
}

void ReadMEMSDataCodec_Server::setTcpCodec(TcpCodec* tcpCodec)
{

  this->tcpCodec=tcpCodec;
  printInjection(this,tcpCodec);
}

void ReadMEMSDataCodec_Server::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setMapper_MEMSData(getObjFromMap<Mapper_MEMSData>(objMap));
  setTcpCodec(getObjFromMap<TcpCodec>(objMap));
  ObjAutoInject::objectAutoInjection(objMap);
}
